/*****************************************************************************
 * $Workfile:: switches.c                                                    $
 * $Revision:: 1                                                             $
 * $JustDate::  4/05/10                                                      $
 *
 *      GENIE V ECU
 *      main loop
 *****************************************************************************/

#include "common.h"
#include "main.h"
#include "switches.h"
// Push-button interface

struct switches SWITCH_Data;


/*******************************************************************************
 *
 * PROCEDURE:  switches()
 *
 * DESCRIPTION: It reads all inputs and debounce them over 4 cycles.
 *              It is invoked once per system cycle 10 milliseonds
 *
 * VARIABLE NAME  R/W DESCRIPTION
 * ---------------- --- --------------------------------------------------------
 * SWITCH_Data        R/W owner
 ******************************************************************************/

void
switches(void)
{
   union
   {
      UINT16 word;
      __bitf_T2 bit;
   } wKeys;

   wKeys.bit.no0 = u8GetStatus2B();      //2B-KEYSWITCH
   wKeys.bit.no1 = u8GetStatus10C();      //10C-LEFT_STEER_TURN_LIMIT
   wKeys.bit.no2 = 0;	  
   wKeys.bit.no3 = 0;
   wKeys.bit.no4 = u8GetStatus1A();      //1A-1A/INPUT
   wKeys.bit.no5 = u8GetStatus11B();      //11B-UP_LIMIT
   wKeys.bit.no6 = u8GetStatus12C();      //12C-DOWN_LIMIT
   wKeys.bit.no7 = u8GetStatus10A();      //10A-=FREEWHEEL
   wKeys.bit.no8 = u8GetStatus12B();      //12B-CHASSIS_UP
   wKeys.bit.no9 = u8GetStatus11C();      //11C-TILT_SENSOR
   wKeys.bit.no10 = 0;
   wKeys.bit.no11 = u8GetStatus10B();     //10B-POTHOLE_LIMIT
   wKeys.bit.no12 = u8GetStatus12A();     //12A-CHASSIS_DN
   wKeys.bit.no13 = 0;
   wKeys.bit.no14 = u8GetStatus5B();     //5B-MOTOR_FAULT_1
   wKeys.bit.no15 = u8GetStatus5C();     //5C-MOTOR_FAULT_2

   /************************************************************************
   2012-04-11 BAH
       To fix the situation where the Pothole Guards deploy is vastly different
       from where Down Limit switch releases, the Zapi's Cutback 1 signal source
       was changed from the Down Limit switch to the Pothole Gaurd down switch.
       This requires that the Pothole Guard switch use the normally open contact
       instead of the normally closed one, thus the polarity of the Pothole guard
       signal is changing polarity for the ELECTRIC 3 lifts.

   */
   if(ELEC3 == lift_setup.bLiftType)
   {
      wKeys.word ^= 0x0800;   // Invert bit 11, the Pothole Guard switch.
   }

   SWITCH_Data.awSwitches[ SWITCH_Data.index++ & (SWITCHES_COUNT - 1)] = wKeys.word;

   wKeys.word = SWITCH_Data.wSwitches.word;

   SWITCH_Data.wSwitches.word |=
      (SWITCH_Data.awSwitches[ 0]
       & SWITCH_Data.awSwitches[ 1]
       & SWITCH_Data.awSwitches[ 2]
       & SWITCH_Data.awSwitches[ 3]);


   SWITCH_Data.wSwitches.word &=
      (SWITCH_Data.awSwitches[ 0]
       | SWITCH_Data.awSwitches[ 1]
       | SWITCH_Data.awSwitches[ 2]
       | SWITCH_Data.awSwitches[ 3]);

   /*
    * set flags for on and off events.
    */
   SWITCH_Data.wSwitchesOn.word =  SWITCH_Data.wSwitches.word & ~wKeys.word;
   SWITCH_Data.wSwitchesOff.word = ~SWITCH_Data.wSwitches.word & wKeys.word;
}

